package com.geez.bbl.pdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author biniam.gebremichael
 * @since 1/2/12
 */
public class TableOfContent extends PdfPageEventHelper {
    List<Phrase> titles = new ArrayList<Phrase>();

    public void onChapter(PdfWriter writer, Document document, float position, Paragraph title) {
        titles.add(new Paragraph(title.getContent(), Bbl2Pdf.nyala));
    }

    public void onChapterEnd(PdfWriter writer, Document document, float position) {
        drawLine(writer.getDirectContent(), document.left(), document.right(), position - 5);
    }

    public void onSection(PdfWriter writer, Document document, float position, int depth, Paragraph title) {
        Phrase title2 = new Phrase(title.getContent(), Bbl2Pdf.nyala);
        //  title.setIndentationLeft(18 * depth);
        titles.add(title2);
    }

    public void onSectionEnd(PdfWriter writer, Document document, float position) {
        drawLine(writer.getDirectContent(), document.left(), document.right(), position - 3);
    }

    public void drawLine(PdfContentByte cb, float x1, float x2, float y) {
        cb.moveTo(x1, y);
        cb.lineTo(x2, y);
        cb.stroke();
    }

}
